package org.belkodevelop.journal.service;

import org.belkodevelop.journal.domain.JournalItem;
import org.belkodevelop.journal.dao.JournalDAO;
import org.belkodevelop.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class JournalService {

    @Autowired
    private JournalDAO journalDAO;

    @Transactional
    public List<JournalItem> getJournalItems(Integer start, Integer end, Long userId) {
        return journalDAO.getJournalItems(start, end, userId);
    }

    @Transactional
    public List<JournalItem> getJournalItemsDate(Date start, Date end, Long userId) {
        return journalDAO.getJournalItems(start, end, userId);
    }

    @Transactional
    public void addJournalItem(JournalItem journalItem) {
        journalDAO.addJournaItem(journalItem);
    }

    @Transactional
    public boolean deleteJournalItem(long id, User owner) {
        JournalItem journalItem = journalDAO.getJournalItem(id);
        if (journalItem != null && journalItem.getUser().equals(owner)) {
            journalDAO.deleteJournalItem(id);
            return true;
        }
        return false;
    }
}
